﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace opentk_first_project
{
    class PreciseTimer
    {
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceFrequency(ref long
        PerformanceFrequency);
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceCounter(ref long
        PerformanceCount);

        long _ticksPerSecond = 0;
        long _previousElapsedTime = 0;
        long _elapsedTicks = 0;
        public long TicksPerSecond { get { return _ticksPerSecond; } }
        public long ElapsedTicks { get { return _elapsedTicks; } }
        public PreciseTimer()
        {
            QueryPerformanceFrequency(ref _ticksPerSecond);
            GetElapsedTime(); // Get rid of first rubbish result
        }

        
        public double GetElapsedTime()
        {
            long time = 0;
            QueryPerformanceCounter(ref time);
            _elapsedTicks = time - _previousElapsedTime;
            double elapsedTime = (double)(_elapsedTicks) /
            (double)_ticksPerSecond;
            _previousElapsedTime = time;
            return elapsedTime;
        }
    }
}
